package com.example.xjttoboxuegu.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjttoboxuegu.R;
import com.example.xjttoboxuegu.activity.LoginActivity;
import com.example.xjttoboxuegu.activity.SettingActivity;
import com.example.xjttoboxuegu.util.AnalysisUtils;

public class MyInfoView {

    public ImageView iv_head_icon;

    private LinearLayout ll_head;
    private RelativeLayout rl_course_history,rl_setting;
    private TextView tv_user_name;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;

    public MyInfoView(Activity context) {
        mContext = context;
        //为之后将Layout转化为view时用
        mInflater = LayoutInflater.from(mContext);
    }

    private void createView() {
        initView();
    }

    private void initView() {
        //设置布局文件
        mCurrentView = mInflater.inflate(R.layout.main_view_myinfo, null);
        ll_head = mCurrentView.findViewById(R.id.ll_head);
        iv_head_icon = mCurrentView.findViewById(R.id.iv_head_icon);
        rl_course_history = mCurrentView.findViewById(R.id.rl_course_history);
        rl_setting = mCurrentView.findViewById(R.id.rl_setting);
        tv_user_name = mCurrentView.findViewById(R.id.tv_user_name);
        mCurrentView.setVisibility(View.VISIBLE);
        setLoginParams(readLoginStatus());//设置登录时界面控件的状态

        ll_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()) {
                    //已登录跳转到个人资料
                    Toast.makeText(mContext,"个人资料",Toast.LENGTH_SHORT).show();
                } else {
                    //未登录跳转到登录
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });

        rl_course_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()) {
                    //跳转到播放记录
                    Toast.makeText(mContext,"历史",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext,"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });

        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()) {
                    //跳转到设置
                    Toast.makeText(mContext,"设置",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext, SettingActivity.class);
                    mContext.startActivityForResult(intent,1);
                } else {
                    Toast.makeText(mContext,"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /**
     * 登录成功后设置我的界面
     * @param isLogin
     */
    public void setLoginParams(boolean isLogin) {
        if (isLogin) {
            tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
        }else {
            tv_user_name.setText("点击登录");
        }

    }

    /**
     * 获取当前导航栏上方显示的view
     * @return
     */
    public View getView() {
        if (mCurrentView == null) {
            createView();
        }
        return mCurrentView;
    }

    /**
     * 显示当前导航栏上方对应的view
     */
    public void showView() {
        if (mCurrentView == null) {
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }


    /**
     * 从SharedPreferences中读取登录状态
     * @return
     */
    private boolean readLoginStatus() {

        SharedPreferences sp = mContext.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);

        boolean isLogin = sp.getBoolean("isLogin",false);
        return isLogin;
    }


}
